import { CDHuangXiaoxi } from "../excel/CDHuangXiaoxi";
import { HuangXiaoxi } from "../struct/HuangXiaoxi";
import { Player } from "../struct/Player";
import { SDPlayer } from "./SDPlayer";
import { SDFuction } from "./SDFuction";
import { E_HuangXiaoxiUseType } from "../defs/huang_xiao_xi/E_HuangXiaoxiType";
import { CEHuangXiaoxiGlobal } from "../excel_extend/CEHuangXiaoxiGlobal";
import { CDFavorSkill } from "../excel/CDFavorSkill";
import { E_PrayEffectType } from "../defs/lord/E_PrayEffectType";
import { CDMath } from "../excel/CDMath";
import { CDHuangXiaoxiFavor } from "../excel/CDHuangXiaoxiFavor";

/**
 * 黄小西系统服务端数据管理
 */
export abstract class SDHuangXiaoxi {
    /**
     * 获取指定类型的加成
     * @param player 
     * @param type 
     * @returns 
     */
    public static getEffectByType(player: Player, type: E_PrayEffectType): number {
        let v = 0;
        if (player) {
            const ks = player.huangXiaoxiManager.getHuangXiaoxiListKeys();
            const vs = player.huangXiaoxiManager.getHuangXiaoxiListValues();
            for (let i = 0, len = ks.length; i < len; i++) {
                const config = CDFavorSkill.getData(ks[i]);
                if (!config || type !== config.effectType) {
                    continue;
                }
                const level = vs[i].favorLevel;
                const added = config.param + config.paramGrow * (level - 1);
                v += added;
            }
        }
        return v;
    }


    /**
     * 添加属性
     * @param player 
     */
    public static addMath(player: Player, outList: number[]): void {

        const ks = player.huangXiaoxiManager.getHuangXiaoxiListKeys();
        const vs = player.huangXiaoxiManager.getHuangXiaoxiListValues();

        for (let i = 0, len = ks.length; i < len; i++) {
            const config = CDFavorSkill.getData(ks[i]);
            if (!config || E_PrayEffectType.AddMath !== config.effectType) {
                continue;
            }
            const mathId = config.param + config.paramGrow * (vs[i].favorLevel - 1);
            const mathConfig = CDMath.getData(mathId);
            if (!mathConfig) {
                continue;
            }
            SDPlayer.addMathConfig(mathConfig, outList);
        }
    }
    /**
     * 获取黄小西技能
     */
    public getHuangXiaoxiSkill(hxx: HuangXiaoxi | CDHuangXiaoxi | number): CDFavorSkill[] {
        let skillids: number[] = [];
        if (hxx instanceof HuangXiaoxi) {
            skillids = CDHuangXiaoxi.getData(hxx.huangXiaoxiId).favorSkillid;
        } else if (hxx instanceof CDHuangXiaoxi) {
            skillids = hxx.favorSkillid;
        } else {
            skillids = CDHuangXiaoxi.getData(hxx).favorSkillid;
        }
        let skills: CDFavorSkill[] = [];
        for (let i = 0; i < skillids.length; i++) {
            let skillid = skillids[i];
            let skill = CDFavorSkill.getData(skillid);
            skills.push(skill);
        }
        return skills;
    }
    /**
     * 获取spine路径
     * @param pet
     * @param player
     */
    public static getSpineUrl(hxx: HuangXiaoxi | CDHuangXiaoxi | number): string {
        let config: CDHuangXiaoxi = null;
        if (hxx instanceof HuangXiaoxi) {
            config = CDHuangXiaoxi.getData(hxx.huangXiaoxiId);
        } else if (hxx instanceof CDHuangXiaoxi) {
            config = hxx;
        } else {
            config = CDHuangXiaoxi.getData(hxx);
        }
        if (config) {
            return "huangxiaoxi/" + config.model + '/' + config.model;
        }
        return null;
    }

    /**
     * 检查玩家是否可以解锁指定黄小西
     */
    public static canUnlockHuangXiaoxi(player: Player, huangXiaoxiId: number): any {
        const config = CDHuangXiaoxi.getData(huangXiaoxiId);
        if (!config) {
            return {
                c: 4001,
                e: "黄小西配置不存在"
            };
        }
        const isUnlock = SDFuction.isUnlock(player, config.unlockid);
        if (!isUnlock) {
            return {
                c: 4001,
                e: "解锁条件未满足"
            };
        }
        const huangxiaoxi = player.huangXiaoxiManager.getHuangXiaoxiList(huangXiaoxiId);
        // 检查是否已经解锁
        if (huangxiaoxi) {
            return {
                c: 4001,
                e: "黄小西已解锁"
            };
        }
        // 检查解锁条件
        return {
            c: 0,
            e: "黄小西解锁成功"
        };
    }
    /**
     * 检查玩家是否可以使用指定好感度道具
     */
    public static canUseFavorItem(player: Player, huangXiaoxiId: number, itemId: number, useType: E_HuangXiaoxiUseType): any {
        //判断使用类型是否正确
        if (!useType || typeof useType !== 'number') {
            useType = E_HuangXiaoxiUseType.Use1;
        }
        //判断物品配置是否存在
        const config = CDHuangXiaoxi.getData(huangXiaoxiId);
        if (!config) {
            return {
                c: 4001,
                e: "黄小西配置不存在"
            };
        }
        // 检查玩家是否拥有指定好感度道具
        const itemNum = player.getItemNum(itemId);
        if (itemNum <= 0) {
            return {
                c: 4001,
                e: "玩家没有足够数量的指定好感度道具"
            };
        }
        let userConfig = CEHuangXiaoxiGlobal.getHuangXiaoxiFavorItemModeNum(useType);
        let useNum = userConfig.value;
        if (useType == E_HuangXiaoxiUseType.Use1) {
            useNum = 1;
        } else if (useType == E_HuangXiaoxiUseType.Use10) {
            if (itemNum < useNum) {
                useNum = itemNum;
            } else {
                useNum = useNum;
            }
        }

        //判断玩家是否用于黄小西
        const huangXiaoxi = player.huangXiaoxiManager.getHuangXiaoxiList(huangXiaoxiId);
        if (!huangXiaoxi) {
            return {
                c: 4001,
                e: "黄小西未解锁"
            };
        }
        return {
            c: 0,
            useNum: useNum,
            e: "玩家可以使用指定好感度道具"
        };
    }

    /**
     * 获取黄小西当前等级的经验进度。
     * @param player 玩家
     * @param huangXiaoxiId 黄小西ID
     * @returns 进度值，小数。
     */
    public static getHuangXiaoxiLevelProgress(player: Player, huangXiaoxiId: number): number {
        const hxx = player.huangXiaoxiManager.getHuangXiaoxiList(huangXiaoxiId);
        const level = hxx.favorLevel;
        const expTotal = hxx.favorExp;
        const config = CDHuangXiaoxiFavor.getData(level);
        if (!config || config.expRequire == -1) {
            return 0;
        }
        const expStart = config.expTotal - config.expRequire;
        const expNow = expTotal - expStart;
        return expNow / config.expRequire;
    }



}
